<?php
if (!defined("MCTDESIGN")) die("#");
require_once './_classes/_class_upload.php';
$upLoad = new UploadFile;

function _list_select_Cat($_catid="") {
	global $mysql, $tb_fix;
	$q = $mysql->query("SELECT * FROM ".$tb_fix."_catalogy_post WHERE cat_parent='$parent' ORDER BY cat_order ASC");
	$t = $mysql->num_rows($q);
	$_list = "";
	if ($t>0) {
		while ($r = $mysql->fetch_array($q)) {
			if ($_catid == $r["cat_id"]) $_list .= "<option value='".$r["cat_id"]."' selected>".$r["cat_title"]."</option>";
			else $_list .= "<option value='".$r["cat_id"]."'>".$r["cat_title"]."</option>";
		}
	}
	return $_list;
}

if (isset($_POST['delete']) || isset($_POST['flag'])) {

	$total = isset($_POST["total"]) ? intval($_POST["total"]) : 0;

 	for($i=1;$i<=$total;$i++){

		$checkbox[$i] = isset($_POST["checkbox_$i"]) ? $_POST["checkbox_$i"] : '';

		if (!empty($checkbox[$i])) {

			$mysql->query("DELETE FROM ".$tb_fix."_news_post WHERE sid='".$checkbox[$i]."'");

		}

	}

	$html_body = _html_trans("Đã xoá bài viết thành công !");

	return false;

}

if (isset($_POST['update'])) {

	$post_userid = isset($_POST["post_userid"]) ? fixquotes(stripslashes($_POST["post_userid"])) : "";

	$post_source = isset($_POST["post_source"]) ? fixquotes(stripslashes($_POST["post_source"])) : "";

	$post_title = isset($_POST["post_title"]) ? fixquotes(stripslashes($_POST["post_title"])) : "";

	$post_catid = isset($_POST["post_catid"]) ? intval($_POST["post_catid"]) : '';

	$post_desc = isset($_POST["post_desc"]) ? fixquotes(stripslashes($_POST["post_desc"])) : "";

	$post_content = isset($_POST["post_content"]) ? stripslashes($_POST["post_content"]) : "";// chú ý dùng thé này không ổn vì nó sẽ loại bỏ quá nhiều dẫn đến khi lấy ra sẽ bị lỗi  fixquotes(m_htmlchars(stripslashes($_POST["post_content"]))

	$post_notes = isset($_POST["post_notes"]) ? fixquotes(stripslashes($_POST["post_notes"])) : "";

	$post_imgurl = isset($_POST["post_imgurl"]) ? fixquotes(stripslashes($_POST["post_imgurl"])) : "";

	$post_img = isset($_FILES['post_img']['name'] ) ? $_FILES['post_img']['name'] : '';

	$post_tmp_img = isset($_FILES['post_img']['tmp_name'] ) ? $_FILES['post_img']['tmp_name'] : '';

	if (empty($post_title) || empty($post_desc) || empty($post_content) || empty($id)) {

		$html_body = _html_trans("Dữ liệu bạn nhập chưa đầy đủ !");

		return false;

	}

	if (!empty($post_img)) {
        $post_img = $upLoad->doSinple("news",$_FILES['post_img']);  
		//$post_img = uploadIMG($post_img,$post_tmp_img,"news");

		$SQL_IMG = ",images='$post_img'";

	} else {

		$SQL_IMG = ",images='$post_imgurl'";

	}

    $mysql->query("UPDATE ".$tb_fix."_news_post SET catid='1',catpid='1',userid='$post_userid',title='$post_title',content_desc='$post_desc',content='$post_content',notes='$post_notes',imgtext='$post_imgtext',source='$post_source' $SQL_IMG WHERE sid='".$id."'");

	$html_body = _html_trans("Đã cập nhật bản tin thành công !","admin.php?obj=Post");

	return false;

}



if (isset($_POST['add'])) {

	$post_userid = isset($_POST["post_userid"]) ? fixquotes(stripslashes($_POST["post_userid"])) : "";

	$post_source = isset($_POST["post_source"]) ? fixquotes(stripslashes($_POST["post_source"])) : "";

	$post_title = isset($_POST["post_title"]) ? fixquotes(stripslashes($_POST["post_title"])) : "";

	$post_catid = isset($_POST["post_catid"]) ? intval($_POST["post_catid"]) : 0;

	$post_desc = isset($_POST["post_desc"]) ? fixquotes(stripslashes($_POST["post_desc"])) : "";

	$post_content = isset($_POST["post_content"]) ? stripslashes($_POST["post_content"]) : "";

	$post_notes = isset($_POST["post_notes"]) ? fixquotes(stripslashes($_POST["post_notes"])) : "";

	$post_imgurl = isset($_POST["post_imgurl"]) ? fixquotes(stripslashes($_POST["post_imgurl"])) : "";

	$post_img = isset($_FILES['post_img']['name'] ) ? $_FILES['post_img']['name'] : '';

	$post_tmp_img = isset($_FILES['post_img']['tmp_name'] ) ? $_FILES['post_img']['tmp_name'] : '';

	if (empty($post_title) || empty($post_desc) || empty($post_content)) {

		$html_body = _html_trans("Dữ liệu bạn nhập chưa đầy đủ !");

		return false;

	}
    //echo $post_desc;exit();
	if (!empty($post_img)) {
        $post_img = $upLoad->doSinple("news",$_FILES['post_img']);
		//$post_img = uploadIMG($post_img,$post_tmp_img,"news");

	}

	else $post_img = $post_imgurl;


	$timenow = time();

	$mysql->query("INSERT INTO ".$tb_fix."_news_post (sid , catid, catpid, admid, userid, title, time, content_desc, content, images, notes, imgtext, source) VALUES ('','1','1','".$admin_id."','".$post_userid."','".$post_title."','".$timenow."','".$post_desc."','".$post_content."','".$post_img."','".$post_notes."','".$post_imgtext."','".$post_source."')");

	$html_body = _html_trans("Đã thêm bản tin thành công !","admin.php?obj=Post");

	return false;

}

function _html_managePost()

{

	global $dir_tpl, $mysql, $tb_fix, $page, $temp,$anyDB;
// get lis category
    $sql = "select cat_id,cat_title from ".$tb_fix."_catalogy_post ";
    $qq = $anyDB->query($sql);
    $arrrcategory = $anyDB->getAll($qq);
    $temp->assign('category',$arrrcategory);

if (isset($_GET['timkiem'])){
    $keywork = $_POST['cate_post'];
    if($keywork != 0){
        $where = " and p.catid = $keywork ";
        $temp->assign('keywork',$keywork);
    }else{
        $where = "";
    }
  // echo $where; die(); 
}


	$_limit = 20;

	$temp_body = $temp;

	$temp_body->assign( 'base_tpl', "".$dir_tpl."" );

	$q = $mysql->query("SELECT * FROM ".$tb_fix."_news_post AS p, ".$tb_fix."_admin AS a, ".$tb_fix."_catalogy_post AS cp WHERE p.admid=a.a_id AND p.catid=cp.cat_id $where ORDER BY sid DESC");

	//$q = $mysql->query("SELECT * FROM ".$tb_fix."_news_post AS p, ".$tb_fix."_admin AS a WHERE p.admid=a.a_id ORDER BY sid DESC");

	$t = $mysql->num_rows($q);

	if ($t>0) {

		$_start = ($page-1) * $_limit ;

		$_end   = $page*$_limit;

		$num_pages = ceil($t/$_limit) ;

		if ($page > $num_pages) $page=$num_pages;

		if ($page < 1 ) $page=1;

		$page_view = Paginate_Admin($t, $_limit, $page, "page");

		$html = "";

		$i = 0;

		$_c = 0;

		while ($r = $mysql->fetch_array($q)) {

			if ($i>=$_start) {

				$_c++;

				if ($r["np_focus"] == 1) {

					$focus = "<font color=\"green\"><b>YES</b></font>";

				}

				else $focus = "<font color=\"red\">NO</font>";

				$html .= "<tr class=\"row1\">";

				$html .= "<td style='width: 3%'><input type='checkbox' id='checkbox' name='checkbox_".$_c."' value='".$r["sid"]."'></td>";

				$html .= "<td width=\"60%\" align=\"left\"><div style=\"margin-left:5px;\"><a href=\"admin.php?obj=Post&act=edit&id=".$r["sid"]."\">".$r["title"]."</div></td>";

				$html .= "<td style='width: 10%' align='center'>".gmdates($r["time"])."</td>";
				$html .= "<td style='text-align: center; width: 11%'>
                                        <a href=\"admin.php?obj=Post&act=edit&id=".$r["sid"]."\" title=\"Sửa tin tức\" class=\"btn_no_text btn ui-state-default ui-corner-all tooltip\">
                									<span class=\"ui-icon ui-icon-wrench\"></span>
            								</a>
                                            <a title=\"Xóa tin tức\" href=\"javascript:;\" onclick=\"check_del_row(".$r["sid"].")\" class=\"btn_no_text btn ui-state-default ui-corner-all tooltip\">
            							<span class=\"ui-icon ui-icon-circle-close\"></span>
            			</a>
                </td>";
				$html .= "</tr>";

			}

			$i++;

			if ($i>=$_end) break;

		}

		$html .= "<input type='hidden' name='total' value='".$_c."'>";

		$html .= "<tr class=\"row1\">";

	

		$html .= "<td align=\"left\" colspan=\"6\">
                <input class=\"ui-state-default ui-corner-all\" name='delete' type='submit' value=' Xoá ' onClick=\"return check_checkbox();\">
				<input class=\"ui-state-default ui-corner-all\" name=\"CheckAll\" type=\"button\" id=\"CheckAll\" value=\"Đánh Dấu Tất Cả\" onClick=\"checkall()\">
				<input class=\"ui-state-default ui-corner-all\" name=\"UnCheckAll\" type=\"button\" id=\"UnCheckAll\" value=\"Bỏ Đánh Dấu\" onClick=\"uncheckall()\">

		</td>";

		$html .= "</tr>";

		//$html .= "<tr height=\"25\" class=\"row1\"><td colspan=\"6\" align='center'>$page_view</td></tr>";

		$temp_body->assign( 'list_post', "".$html."" );
        $temp_body->assign( 'page_view', "".$page_view."" );
	}

	else $temp_body->assign( 'list_post', "<tr><td colspan=\"6\" align=\"center\" style=\"text-align: center; padding: 10px\">Chưa có bản tin nào !</td></tr>" );
    $temp_body->assign("breakcum",breakcum("Danh sách tin tức "));
	$_html = $temp_body->fetch( '_adm_news_post.mct' );

	return $_html;

}



function _html_addPost()

{

	global $dir_tpl, $mysql, $tb_fix,$temp;

	$temp_body = $temp;

	$temp_body->assign( 'base_tpl', "".$dir_tpl."" );
	$list_cat = _list_select_Cat();
	$temp_body->assign( 'list_cat', "".$list_cat."" );
    $temp_body->assign("breakcum",breakcum("Thêm Bài Viết "));
	$_html = $temp_body->fetch( '_adm_news_post_add.mct' );

	return $_html;

}

function _html_editPost($_id="")

{

	global $dir_tpl, $mysql, $tb_fix, $temp;

	$q = $mysql->query("SELECT * FROM ".$tb_fix."_news_post AS p, ".$tb_fix."_admin AS a WHERE p.admid=a.a_id AND p.sid='$_id' ORDER BY sid DESC");

	$t = $mysql->num_rows($q);

	$_html = "";

	if ($t>0) {

		$r = $mysql->fetch_array($q);

		$temp_body = $temp;
		$list_cat = _list_select_Cat($r["catid"]);
		$temp_body->assign( 'list_cat', "".$list_cat."" );

		$temp_body->assign( 'base_tpl', "".$dir_tpl."" );

		$temp_body->assign(array(

								"post_userid" => "".$r["userid"]."",

								"post_source" => "".$r["source"]."",

								"post_title" => "".txt_unHTML($r["title"])."",

								"post_desc" => "".unbr($r["content_desc"])."",

								"post_content" => "".htmlchars($r["content"])."",

								"post_time" => "Ngày : ".gmdates($r["time"])."",

								"post_notes" => "".$r["notes"]."",

								"post_imgtext" => "".$r["imgtext"]."",

								"post_images" => "".$r["images"]."",

								"post_id" => "".$r["sid"].""

							)

						);
        $temp_body->assign("breakcum",breakcum("Sửa bản tin "));
		$_html = $temp_body->fetch( '_adm_news_post_edit.mct' );

	}

	else {

		$_html .= _html_trans("Không tồn tại bản tin này trong hệ thống !","admin.php?obj=Post");

	}

	return $_html;

}

if ($act=="add") {

	$html_body = _html_addPost();

	return false;

}

if ($act=="edit") {

	$html_body = _html_editPost($id);

	return false;

}

$html_body = _html_managePost();



?>